{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=\"/data/dataset/vqav2/vqa_dataset/annotations/v2_mscoco_val2014_annotations.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已成功提取前1000行数据并保存到 v2_mscoco_val2014_annotations_1000.json\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# 输入和输出文件路径\n",
    "input_file = \"/data/dataset/vqav2/vqa_dataset/questions/v2_OpenEnded_mscoco_val2014_questions.json\"\n",
    "output_file = \"v2_OpenEnded_mscoco_val2014_questions_1000.json\"\n",
    "\n",
    "# 读取原始JSON文件\n",
    "with open(input_file, 'r', encoding='utf-8') as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "# 提取前1000行数据\n",
    "# 假设JSON文件是一个列表，每个元素是一行数据\n",
    "if isinstance(data, list):\n",
    "    extracted_data = data[:1000]\n",
    "else:\n",
    "    # 如果JSON文件是一个字典，我们需要根据具体结构来处理\n",
    "    # 这里假设数据在'annotations'键下\n",
    "    if 'annotations' in data:\n",
    "        extracted_data = {'annotations': data['annotations'][:1000]}\n",
    "        # 保留其他必要的键\n",
    "        for key in data:\n",
    "            if key != 'annotations':\n",
    "                extracted_data[key] = data[key]\n",
    "    else:\n",
    "        raise ValueError(\"无法识别的JSON结构\")\n",
    "\n",
    "# 保存提取的数据到新文件\n",
    "with open(output_file, 'w', encoding='utf-8') as f:\n",
    "    json.dump(extracted_data, f, ensure_ascii=False, indent=2)\n",
    "\n",
    "print(f\"已成功提取前1000行数据并保存到 {output_file}\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llava",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
